package com.totoro.mall.mall_buyer.service.impl;

import com.totoro.mall.mall_buyer.dto.CartDTO;
import com.totoro.mall.mall_buyer.enums.ProductStatusEnum;
import com.totoro.mall.mall_buyer.enums.ResultEnum;
import com.totoro.mall.mall_buyer.exception.SellException;
import com.totoro.mall.mall_buyer.model.ProductInfo;
import com.totoro.mall.mall_buyer.repository.ProductInfoRepository;
import com.totoro.mall.mall_buyer.service.IProductInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.awt.print.Pageable;
import java.util.List;

/**
 * Created by Niki on 2018/4/12 8:05
 */
@Service
@Slf4j
public class ProductInfoServiceImpl implements IProductInfoService {
    @Resource
    ProductInfoRepository productInfoRepository;

    @Override
    public ProductInfo findOne(String productId) {
        return productInfoRepository.getOne(productId);
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return productInfoRepository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findUpAll(Pageable pageable) {
        return productInfoRepository.findByProductStatus(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo product) {
        return productInfoRepository.save(product);
    }

    /*修改动作需要加上事务*/
    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOS) throws SellException{
        for (CartDTO cart : cartDTOS) {
            ProductInfo product = productInfoRepository.getOne(cart.getProductId());
            if(product == null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer newStock = product.getProductStock() + cart.getProductQuantity();
            product.setProductStock(newStock);
            productInfoRepository.save(product);
        }

        // TODO: 2018/4/16 lambda中抛异常的问题
//        cartDTOS.stream().forEach(e -> {
//            ProductInfo product = productInfoRepository.getOne(e.getProductId());
//            if(product == null){
//                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
//            }
//            Integer newStock = product.getProductStock() + e.getProductQuantity();
//            product.setProductStock(newStock);
//            productInfoRepository.save(product);
//        });
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOS) throws SellException{
        for (CartDTO cart : cartDTOS) {
            ProductInfo product = productInfoRepository.getOne(cart.getProductId());
            if (product == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            Integer newStock = product.getProductStock() - cart.getProductQuantity();
            if (newStock < 0) {
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            product.setProductStock(newStock);
            productInfoRepository.save(product);
        }

//        cartDTOS.stream().forEach(e->{
//            ProductInfo product = productInfoRepository.getOne(e.getProductId());
//            if (product == null) {
//                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
//            }
//
//            Integer newStock = product.getProductStock() - e.getProductQuantity();
//            if (newStock < 0) {
//                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
//            }
//            product.setProductStock(newStock);
//            productInfoRepository.save(product);
//        });
    }

    @Override
    public ProductInfo onSale(String productId) throws SellException{
        ProductInfo product = productInfoRepository.getOne(productId);
        if (product == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        product.setProductStatus(ProductStatusEnum.UP.getCode());
        return productInfoRepository.save(product);
    }

    @Override
    public ProductInfo offSale(String productId) throws SellException{
        ProductInfo product = productInfoRepository.getOne(productId);
        if (product == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        product.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return productInfoRepository.save(product);
    }
}
